/**
 * 同路人
 */
if(!$.cookie("MYID")){location.href="https://www.pulu.com/weixin/login.html"};
var currentIndex=1,pageSize=10,canload=true;
//环信建立连接
var conn = new WebIM.connection({
    https: WebIM.config.https,
    url: WebIM.config.xmppURL,
    isAutoLogin: WebIM.config.isAutoLogin,
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions
});
//登录
var options = {
    apiUrl: WebIM.config.apiURL,
    user:id,
    pwd:id,
    appKey: "pulu#pulu"
};
conn.open(options);
//添加好友
function addfri(e){
    if(e.className==="straddbtn"){
        e.className="straddbtn clicked";
        //发送好友模板
        $.ajax({
            type:"post",
            url:savefriendnoteurl0,
            data:{"userId": $.cookie("MYID"),"friendId":e.dataset.uid,"msg":""},
            success:function(d){

            }
        });
        conn.subscribe({
            to: e.dataset.uid,
            message: $.cookie("MYNICK")+"请求添加你为好友"
        });
    }
}
//查看全部
function seeall(e){
    console.log(e.dataset.uid);
    $.cookie("otherid",e.dataset.uid);
    location.href="lastinfo.html";
}
//新闻跳转
function newsgo(e){
    //判断能不能去看详情页
    var thiseid= e.dataset.eid;
    var errorUrl=/环球网|1905电影网|中国会计视野|编织人生网|IT之家|优姿美女/;
    if(errorUrl.test(e.dataset.sitename)){
        location.href="http://www.pulu.com/weixin/newsdetail.html?newsid="+thiseid+"#fromuser="+id;
    }
    else{
        location.href="newsdetail.html?newsid="+thiseid+"#fromuser="+id;
    }
}
//
//$.ajax({
//   type:"get",
//    url:"https://www.pulu.com/webapi/api/PuluUser/GetUserScores?userId="+ $.cookie("MYID"),
//    success:function(d){
//        $("#lastdayAddCount").html(d.Data.lastdayAddCount);
//        $("#totaluserCount").html(d.Data.totaluserCount);
//    }
//});
//
function aj(){
    canload=false;
    $.ajax({
        type:"get",
        url:"https://www.pulu.com/webapi/api/puluuser/GetCommonUsers?userid="+id+"&currentIndex="+currentIndex+"&pageSize="+pageSize,
        datatype:"json",
        success:function(data){
            if(data.Data&&data.Data.length>0){
                canload=true;
                var code="";
                for(var i=0;i<data.Data.length;i++){
                    var userimg;//头像
                    if(data.Data[i].headimg){
                        userimg="<img class='topic_photo_img' src='"+imgurl+data.Data[i].headimg+"'/>";
                    }else{
                        userimg="<img class='topic_photo_img' src='img/af8.png'/>"
                    }
                    var sex="<span class='man'></span>";
                    if(data.Data[i].sex==0){
                        sex="<span class='woman'></span>";
                    }
                    //好友 真名
                    var isfriend="",realname="";
                    if(data.Data[i].realName){realname=" ("+data.Data[i].realName+")"}
                    if(!data.Data[i].isfriend){
                        //realname="";
                        isfriend="<div class='straddbtn' data-uid='"+data.Data[i].userId+"' onclick='addfri(this)'>+加好友</div>";
                    }
                    //常看
                    var alwayssee="订阅号：";
                    var a=(data.Data[i].news).concat(data.Data[i].jobs,data.Data[i].hobbys);
                    alwayssee+= a.join(",");
                    //新闻
                    var news="";
                    if(data.Data[i].news){news+=(data.Data[i].news).join(",");}
                    //行业
                    var jobs="";
                    if(data.Data[i].jobs){jobs+=(data.Data[i].jobs).join(",");}
                    //爱好
                    var hobbys="";
                    if(data.Data[i].hobbys){hobbys+=(data.Data[i].hobbys).join(",");}
                    //最新动态
                    var lastnews="";
                    if(data.Data[i].statusLog&&data.Data[i].statusLog.length>0){
                        $(data.Data[i].statusLog).each(function(n,d){
                            var tim=getDateDiff(getDateTimeStamp(d.cTime));
                            if(!tim){tim="刚刚"};
                            if(d.type===1){
                                if(d.title){
                                    lastnews+="<div class='pagedlist_item'> <div class='feed_item feed_type_question'><div class='ContentFooter QuestionFooter rt'> <span class='answer_count'>"+tim+"</span></div><h2 class='no_highlight'> <a class='question_link' onclick='newsgo(this)' data-eid='"+d.qid+"' data-sitename='"+d.sitename+"'>"+d.title+"</a></h2></div></div>";
                                }
                            }else if(d.type===2){
                                lastnews+="<div class='pagedlist_item'> <div class='feed_item feed_type_question'><div class='ContentFooter QuestionFooter rt'> <span class='answer_count'>"+tim+"</span></div><h2 class='no_highlight'> <a class='question_link' href='questiondetail.html?qid="+d.qid+"#fromuser="+id+"'>"+d.title+"</a></h2></div></div>";
                            }
                            else{
                                lastnews+="<div class='pagedlist_item'> <div class='feed_item feed_type_question'><div class='ContentFooter QuestionFooter rt'> <span class='answer_count'>"+tim+"</span></div><h2 class='no_highlight'> <a class='question_link' href='shareexppage.html?eid="+d.qid+"#fromuser="+id+"'>"+d.title+"</a></h2></div></div>";
                            }
                        });
                        var disnon="";
                        if(data.Data[i].statusLog.length<3){disnon="style='display:none'"}
                        lastnews="<div class='listtitle'><div class='seeall rt' onclick='seeall(this)' "+disnon+" data-uid="+data.Data[i].userId+">查看更多</div><div>最新内容</div></div>"+lastnews;
                    }
                    code+="<div class='Cell'> <div class='friends'> <div class='imageplace'> <a href='otherpeoplepage.html?otherid="+data.Data[i].userId+"'>"+userimg+"</a> </div> <div class='innerplace'> <div><div> <a href='otherpeoplepage.html?otherid="+data.Data[i].userId+"' class='tc_text'>"+data.Data[i].nick+realname+sex+"</a>"+isfriend+"</div><a>"+alwayssee+"</a></div></div></div>"+lastnews+"</div>"
                }
                $("#listplace ").append(code);
            }
            else{
                $("#nomore").css("display","block");
                $("#pleasewait1").css("display","none");
            }

        },
        error:function(d){
            console.log(d);
        }
    })
}
aj();
//滚动加载
$(window).scroll(function(){
    var mainheight=$(".home_feed").height();
    var windowheight=$(window).height();
    var chajuli=$(".home_feed").offset().top - $(window).scrollTop();
    var cz=windowheight-mainheight+5;
    //不是选择城市的 滚动效果
    if(canload&&chajuli<cz){
        currentIndex++;
        aj();
    }
});